<?php
class MShuadanshou extends MY_Model {

    protected $table = 'shuadanshou';

    function __construct() {
        parent::__construct();
    }

    public static function getState($state=NULL){
        $define = array('1'=>'正常','0'=>'不可用');
        if($state === NULL){
            return $define;
        }else{
            return key_exists($state, $define)?$define[$state]:'';
        }
    }
    public static function getType($type=NULL){
        $define = array('1'=>'中介','2'=>'个人');
        if($type === NULL){
            return $define;
        }else{
            return key_exists($type, $define)?$define[$type]:'';
        }
    }

    public function getList($limit, $sort=false){
        $this->db->select( 's.*, a.name aname');
        $this->db->distinct();
        $this->db->from($this->table.' s');
        $this->db->join('core_admins a','s.admin_id = a.id', 'LEFT');
        if($this->_user->role_id != 1){
            $this->db->where('s.admin_id',$this->_user->id);
        }
        $this->db->limit($limit['limit'],$limit['offset']);
        $this->formfilter->doFilter();

        if($sort){
            $this->db->order_by('s.sort', 'asc');
        }else{
            $this->db->order_by('s.id', 'desc');
        }
        $query = $this->db->get();
        return $query->result();
    }
    public function getCount(){
        $this->db->select('count(distinct s.id) as num');
        $this->db->from($this->table . ' as s');
        $this->formfilter->doFilter();
        if($this->_user->role_id != 1){
            $this->db->where('s.admin_id',$this->_user->id);
        }
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id){
        $query = $this->db->where('id',$id)->get($this->table);
        return $query->row();
    }

    public function getAllShuadanshous($admin_id){
        $this->db->select('id,name,account');
        $this->db->from($this->table);
        $this->db->where('status',1);
        if($admin_id){
            $this->db->where('admin_id',$admin_id);
        }
        $queryResult = $this->db->get()->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->name.'-'.$v->account;
        }
        return $kvArr;
    }

}
